﻿--在线时间奖励

--#include "data\language\LangCode.txt" once
--#include "data\functions\Activity\Common\Awards.lua" once
--#include "data\config\welfare\OnlineTimeAward.lua" once

-- 开始抽奖
function GetPerOnlineReward(sysarg, rewardId)
	if rewardId < 1 or rewardId > #OnlineTimeAward then
		return 1
	end
	
	local config = OnlineTimeAward[rewardId]
	if not config then
		return 2
	end
	
	local onlineTime = Actor.getTodayOnlineTime(sysarg)
	if onlineTime < config.online then -- 还没到领取时间
		return 3
	end
	
	local var = Actor.getStaticVar(sysarg) 
	if not var.days then var.days = {} end
	local rewardFlags = var.days.dailyOnlineRewardFlag -- 领取在线奖励标志
	if rewardFlags == nil then rewardFlags = 0 end

	local isGot = System.getIntBit(rewardFlags, rewardId - 1)
	if isGot ~= 0 then
		return 4
	end

	local tAward = config.awards

	if IsBagGridEnough(sysarg,tAward) then
		if Awards.Give(sysarg, tAward, GameLog.clOnlineAwardItem, Lang.LogTips.l00081) == false then
			return 6
		end
	else
		return 6
	end
	
	-- 设置领取标志
	rewardFlags = System.setIntBit(rewardFlags, rewardId - 1, true)
	var.days.dailyOnlineRewardFlag = rewardFlags

	return 7,rewardId
end



function GetDailyOnlineReward(sysarg, rewardIndex)
	local GetCount = 0
	local ErrorCode = 0
	local rewardTable = {}
	local Value,rewardId = GetPerOnlineReward(sysarg, rewardIndex)
	if Value == 7 then
		SendDailyOnlineReward(sysarg)

		local npack = DataPack.allocPacket(sysarg, LogicCmd.trSys.sysId, LogicCmd.trSys.sub.sShowRewardIndex)  --申请一个数据包
		DataPack.writeByte(npack, rewardIndex)
		DataPack.flush(npack)
		return
	else	
		ErrorCode = Value
	end
	if ErrorCode == 0 then
		ErrorCode = Value
	end
	

	if ErrorCode == 1 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.OnlineTimeAward001,ttFlyTip)
	elseif ErrorCode == 2 then
	elseif ErrorCode == 3 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.f00004,ttFlyTip)
	elseif ErrorCode == 4 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.zf664, ttFlyTip)
	elseif ErrorCode == 5 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.f00072, ttFlyTip)
	elseif ErrorCode == 6 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
	end
end

function SendDailyOnlineReward(sysarg)
	local var = Actor.getStaticVar(sysarg)
	if not var.days then 
		var.days = {} 
	end
	if not var.days.dailyOnlineRewardFlag  then 
		var.days.dailyOnlineRewardFlag = 0 
	end
	local rewardFlags = var.days.dailyOnlineRewardFlag -- 领取在线奖励标志
	local onlineTime = Actor.getTodayOnlineTime(sysarg)
	local len = #OnlineTimeAward
	local npack = DataPack.allocPacket(sysarg, LogicCmd.trSys.sysId, LogicCmd.trSys.sub.sGetOnlineRewardInfo)  --申请一个数据包
	DataPack.writeUInt(npack, onlineTime)
	DataPack.writeByte(npack, len)
	for idx = 1, len do
		local state = System.getIntBit(rewardFlags, idx - 1) or 0 
		DataPack.writeByte(npack, state)
	end
	DataPack.flush(npack)
end

function DailyOnlineTimeReward(sysarg, rewardType, rewardIndex)
	if rewardType == 0 then
		SendDailyOnlineReward(sysarg)
	elseif rewardType == 1 then
		GetDailyOnlineReward(sysarg, rewardIndex)
	end
end

-- 在线时间奖励
function ClientMsgRewardSystem(sysarg, systemid, msgid, packet)
	if msgid == LogicCmd.trSys.sub.cGetOnlineRewardInfo then
		local rewardType = DataPack.readByte(packet)
		local rewardIndex = DataPack.readByte(packet)
		DailyOnlineTimeReward(sysarg, rewardType, rewardIndex)
	end
end


SystemHandlerDispatcher.registerSystemHander(BaseTypes.SystemId.timeRewardSystem, ClientMsgRewardSystem)

